﻿using HardcoreGame.Content.Buffs;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent;
using Terraria.ModLoader;

namespace HardcoreGame.Common.GlobalNPCs
{
    public class BuffNPC : GlobalNPC
    {
        public static int SleepDebuffType => ModContent.BuffType<Sleep>();
        private readonly Dictionary<int, SleepInfo> sleepingNPCs = [];
        public override bool InstancePerEntity => true;
        protected override bool CloneNewInstances => true;
        public override void PostAI(NPC npc)
        {
            if (npc.HasBuff(SleepDebuffType))
            {
                if (!sleepingNPCs.TryGetValue(npc.whoAmI, out SleepInfo value))
                {
                    value = new SleepInfo(npc);
                    sleepingNPCs[npc.whoAmI] = value;
                }

                ApplySleepEffect(npc);
                value.Update();
            }
            else sleepingNPCs.Remove(npc.whoAmI);
        }
        private static void ApplySleepEffect(NPC npc)
        {
            npc.velocity.X = 0;
            npc.velocity.Y = 0;
            npc.ai[0] = 0;
            npc.ai[1] = 0;
            npc.ai[2] = 0;
            npc.ai[3] = 0;
            npc.frame.Y = 0;
            npc.spriteDirection = npc.direction;
        }
        public override bool PreDraw(NPC npc, SpriteBatch spriteBatch, Vector2 screenPos, Color drawColor)
        {
            if (npc.HasBuff(SleepDebuffType) && sleepingNPCs.TryGetValue(npc.whoAmI, out SleepInfo info))
            {
                info.Draw(spriteBatch);
            }

            return base.PreDraw(npc, spriteBatch, screenPos, drawColor);
        }
    }
    public class SleepInfo
    {
        private readonly NPC target;
        private Vector2 position;
        private float scale = 0.95f;
        private float alpha = 1f;
        private int frame = 0;
        private int frameCounter = 0;
        private readonly string[] zzzTexts = { "z", "z", "z" };

        public SleepInfo(NPC npc)
        {
            target = npc;
            ResetPosition();
        }

        public void ResetPosition()
        {
            float offsetX = target.direction == 1 ? target.width * 0.4f : -target.width * 0.4f;
            position = target.Center + new Vector2(offsetX, -target.height * 0.6f);
        }

        public void Update()
        {
            position.Y -= 0.5f;
            frameCounter++;
            if (frameCounter >= 60)
            {
                frameCounter = 0;
                frame = (frame + 1) % zzzTexts.Length;
                ResetPosition();
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (target.IsABestiaryIconDummy) return;

            string text = zzzTexts[frame];
            Vector2 origin = FontAssets.MouseText.Value.MeasureString(text) / 2f;
            Color color = Color.White * alpha;
            Vector2 shadowPos = position - Main.screenPosition + new Vector2(2f, 2f);
            spriteBatch.DrawString(
                FontAssets.MouseText.Value,
                text,
                shadowPos,
                Color.Black * alpha * 0.7f,
                0f,
                origin,
                scale,
                SpriteEffects.None,
                0f
            );
            spriteBatch.DrawString(
                FontAssets.MouseText.Value,
                text,
                position - Main.screenPosition,
                color,
                0f,
                origin,
                scale,
                SpriteEffects.None,
                0f
            );
        }
    }
}
